﻿using System.Collections.Generic;
using System.Linq;

namespace OdaOperation.Model
{
    public class TableSize
    {
        public List<double> Rows { get; set; }

        public List<double> Cols { get; set; }

        public double Row { get; set; }

        public double Col { get; set; }

        public TableSize()
        {
        }

        public static implicit operator TableSize((double row, double col) value)
        {
            return new TableSize(value.row, value.col);
        }

        public TableSize(double row, double col)
        {
            Row = row;
            Col = col;
        }

        public TableSize(List<double> rows, List<double> cols)
        {
            Rows = rows;
            Cols = cols;
        }

        public void InitFromTable(TableData tableData, int addRow = 0, int addCol = 0)
        {
            Rows = Rows ?? new List<double>();
            Cols = Cols ?? new List<double>();
            if (Row == 0.0)
            {
                if (Rows.Count == 0)
                {
                    Row = 10.0;
                }
                else
                {
                    Row = Rows.LastOrDefault();
                }
            }

            int count = Rows.Count;
            int count2 = Cols.Count;
            int num = tableData.Sum((TableRow x) => x.Max((TableItem y) => y.RowSpan));
            for (int i = count; i < num + addRow; i++)
            {
                Rows.Add(Row);
            }

            if (Col == 0.0)
            {
                if (Cols.Count == 0)
                {
                    Col = 10.0;
                }
                else
                {
                    Col = Cols.LastOrDefault();
                }
            }

            int num2 = tableData.Max((TableRow x) => x.Sum((TableItem y) => y.ColSpan));
            if (tableData.Where((TableRow x) => x.Where((TableItem y) => y.RowSpan != 1).Count() > 0).Count() > 0)
            {
                num2 *= 2;
            }

            for (int j = count2; j < num2 + addCol; j++)
            {
                Cols.Add(Col);
            }
        }
    }
}